const Router = require("koa-router");
const { Auth } = require("@middlewares/auth");
const { Goods } = require("@models/goods");

const router = new Router({
  prefix: "/v1/user/goods",
});
// 商品列表
router.post("/list", async (ctx) => {
  const { page, pageSize } = ctx.request.body;
  const offset = (page - 1) * pageSize;
  where = {
    is_on: 1,
  };
  const { count: total, rows } = await Goods.findAndCountAll({
    where,
    limit: pageSize, // 每页数量
    offset, // 偏移量
    order: [
      ["sort", "desc"],
      ["created_at", "desc"],
    ], // 排序
  });

  ctx.body = {
    code: 200,
    msg: "查询成功",
    total,
    data: rows,
  };
});

// 商品详情
router.post("/detail", async (ctx) => {
  const { goods_id } = ctx.request.body;
  const goods = await Goods.findOne({ where: { goods_id, is_on: 1 } });

  if (!goods) {
    ctx.body = {
      code: 200,
      msg: "查询失败",
      data: null,
    };
  } else {
    ctx.body = {
      code: 200,
      msg: "查询成功",
      data: goods,
    };
  }

 
});

module.exports = router;
